package chapter9;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @author lzy
 * @create 2018-03-20 上午10:47
 */
public class TicketThread implements Runnable {
    private static final Logger LOGGER = LoggerFactory.getLogger(TicketThread.class);

    private int ticket = 100;

    @Override
    public void run() {
        for (int i = 0; i < 120; i++) {
            synchronized (this) {
                if (this.ticket > 0) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    LOGGER.info("[{}] Sell out one ticket, left tickets-{}", Thread.currentThread().getName(), ticket--);
                }
            }
        }
    }
}
